package com.mdxq.mall.sale.service.impl;

import com.mdxq.mall.sale.entity.Coupon;
import com.mdxq.mall.sale.entity.CouponProductCategoryRelation;
import com.mdxq.mall.sale.entity.CouponProductRelation;
import com.mdxq.mall.sale.mapper.CouponMapper;
import com.mdxq.mall.sale.mapper.CouponProductCategoryRelationMapper;
import com.mdxq.mall.sale.mapper.CouponProductRelationMapper;
import com.mdxq.mall.sale.service.CouponService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 优惠券表 服务层实现。
 *
 * @author ShuJie
 * @since 2025-01-12
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>  implements CouponService{
    @Autowired
    private CouponProductCategoryRelationMapper categoryRelationMapper;
    @Autowired
    private CouponProductRelationMapper productRelationMapper;
    @Override
    @Transactional
    public boolean save(Coupon entity) {
        mapper.insert(entity);
        if(entity.getUseType()==1){
            List<CouponProductCategoryRelation>couponProductCategoryRelations=entity.getProductCategoryRelationList();
            for(CouponProductCategoryRelation relation:couponProductCategoryRelations){
                relation.setCouponId(entity.getId());
            }
            return categoryRelationMapper.insertBatch(couponProductCategoryRelations)>0;
        } else if (entity.getUseType()==2) {
            List<CouponProductRelation>couponProductRelations=entity.getProductRelationList();
            for(CouponProductRelation relation:couponProductRelations){
                relation.setCouponId(entity.getId());
            }
            return productRelationMapper.insertBatch(couponProductRelations)>0;
        }
        return true;
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        Coupon entity=super.getById(id);
        super.removeById(id);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("coupon_id",id);
        if(entity.getUseType()==1){
            categoryRelationMapper.deleteByQuery(queryWrapper);
        } else if (entity.getUseType()==2) {
            productRelationMapper.deleteByQuery(queryWrapper);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateById(Coupon entity) {
        super.updateById(entity);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("coupon_id",entity.getId());
        if(entity.getUseType()==1){
            categoryRelationMapper.deleteByQuery(queryWrapper);
            List<CouponProductCategoryRelation>couponProductCategoryRelations=entity.getProductCategoryRelationList();
            for(CouponProductCategoryRelation relation:couponProductCategoryRelations){
                relation.setCouponId(entity.getId());
            }
            return categoryRelationMapper.insertBatch(couponProductCategoryRelations)>0;
        } else if (entity.getUseType()==2) {
            productRelationMapper.deleteByQuery(queryWrapper);
            List<CouponProductRelation>couponProductRelations=entity.getProductRelationList();
            for(CouponProductRelation relation:couponProductRelations){
                relation.setCouponId(entity.getId());
            }
            return productRelationMapper.insertBatch(couponProductRelations)>0;
        }
        return true;
    }

    @Override
    public Coupon getById(Serializable id) {
        Coupon coupon=super.getById(id);
        QueryWrapper queryWrapper=new QueryWrapper();
        if(coupon.getUseType()==1){
            queryWrapper.eq("coupon_id",coupon.getId());
            coupon.setProductCategoryRelationList(categoryRelationMapper.selectListByQuery(queryWrapper));
        } else if (coupon.getUseType()==2) {
            queryWrapper.eq("coupon_id",coupon.getId());
            coupon.setProductRelationList(productRelationMapper.selectListByQuery(queryWrapper));
        }
        return coupon;
    }
}
